﻿/*布线端子检测*/
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using Common;
public class WiringPoints : MonoBehaviour, IPointerClickHandler, IPointerEnterHandler, IPointerExitHandler{
    // Use this for initialization
    Text Notice;
    Texture2D TargetCursor;
    AudioClip success;
    AudioClip fail;
    public AssemblyRun assemblyRun;
    void Start()
    {
        assemblyRun = GameObject.Find("AssemblyRun").GetComponent<AssemblyRun>();
        Notice = GameObject.Find("Notice").GetComponent<Text>();

        //添加声音clip
        success = Resources.Load("Audio/success", typeof(AudioClip)) as AudioClip;
        fail = Resources.Load("audio/fail", typeof(AudioClip)) as AudioClip;
        Debug.Log("wirpoint");
    }

    // Update is called once per frame
    void Update()
    {


    }

    public void OnPointerClick(PointerEventData eventData)
    {
        if (assemblyRun.wiringIndex < Global.WiringObjectsName.Length)
        {
            if (assemblyRun.StartPointName == "")
            {
                assemblyRun.StartPointName = this.name;
                assemblyRun.StartFly = this.transform.position;
                assemblyRun.isFly = 1;
                assemblyRun.FlyLine.SetActive(true);
            }
            else if (assemblyRun.StartPointName != "" && assemblyRun.StartPointName != this.name && assemblyRun.EndPointName == "")
            {
                assemblyRun.EndPointName = this.name;
                assemblyRun.isFly = 0;
                assemblyRun.FlyLine.SetActive(false);
                assemblyRun.FlyLine.transform.position = new Vector3(0, 0, 0);

            }
            if (assemblyRun.StartPointName != "" && assemblyRun.EndPointName != "")
            {
                string[] curWiring = new string[4];
                //将当前布线对象存入临时数组
                for (int i = 0; i < Global.WiringObjectsName[assemblyRun.wiringIndex].Length; i++)
                {
                    curWiring[i] = Global.WiringObjectsName[assemblyRun.wiringIndex][i];
                }

                if (curWiring.Contains(assemblyRun.StartPointName) && curWiring.Contains(assemblyRun.EndPointName))
                {
                    assemblyRun.wiringObjects[assemblyRun.wiringIndex][1].SetActive(true);
                    assemblyRun.wiringIndex++;
                    AudioSource.PlayClipAtPoint(success, this.transform.position);
                    Notice.text = "太棒了，连接正确！加油呀！";
                }
                else
                {
                    Notice.text = "连接错误，请重试！把鼠标指向原理图中闪烁的红线，会有连接点提示哟！";
                    AudioSource.PlayClipAtPoint(fail, this.transform.position);
                }
                assemblyRun.StartPointName = "";
                assemblyRun.EndPointName = "";
                assemblyRun.isFly = 0;
            }

            //如果布线完成则直接返回
            if (assemblyRun.wiringIndex >= Global.WiringObjectsName.Length)
            {
                if (assemblyRun.cables[0])
                {
                    assemblyRun.cables[0].SetActive(true);
                }
                if (assemblyRun.cables[1])
                {
                    assemblyRun.cables[1].SetActive(true);
                }

                MessageBox.Show("提示", "恭喜你！完成所有连线！去看看运行效果吧！", "仿真运行", "重新练习");
                MessageBox.confim = () =>
                {
                    SceneManager.LoadScene("U3_05_Run");
                    Debug.Log("进入仿真");
                };
                MessageBox.cancel = () =>
                {
                    Notice.text = "你已完成全部安装与接线练习，点击按钮可重新开始。";
                };


                //原理图提示闪烁停止
                Destroy(assemblyRun.wiringObjects[Global.WiringObjectsName.Length - 1][0].GetComponent<UITwinking>());
                assemblyRun.wiringObjects[Global.WiringObjectsName.Length - 1][0].GetComponent<CanvasGroup>().alpha = 1;
                //移除连线完成的导线的原理图提示
                Destroy(assemblyRun.wiringObjects[Global.WiringObjectsName.Length - 1][0].GetComponent<WiringHelp>());
            }

        }
    }

    public void OnPointerEnter(PointerEventData eventData)
    {
        //把鼠标指针改为ClickedCursorImg
        TargetCursor = Resources.Load("Cursor/TargetCursor1", typeof(Texture2D)) as Texture2D;
        Cursor.SetCursor(TargetCursor, Vector2.zero, CursorMode.Auto);
        Debug.Log(this.name);
    }

    public void OnPointerExit(PointerEventData eventData)
    {
        // 重置鼠标指针图标
        Cursor.SetCursor(null, Vector2.zero, CursorMode.Auto);
        Debug.Log("mouseout");
    }
}
